<template>
  <div style="width: 600px;background: #fff;">
    <el-breadcrumb separator-class="el-icon-arrow-right">

      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i>后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加用户</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="user"  ref="user" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="name">
        <el-input v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="word">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="order">
        <el-input v-model="user.nick"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('user')">添加</el-button>
        <el-button @click="resetForm('user')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      user: {
        username: '',
        password: '',
        nick: '',
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url="http://localhost:8091/user/add-new"
          console.log('url'+url);
          let formData = this.qs.stringify(this.user)
          this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let responseBody=response.data;
            console.log(responseBody);
            if(responseBody.state==20000){
              this.$message.success("添加成功！");
              this.resetForm();
            }else {
              this.$message.error("添加失败，用户已存在！");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this[formName] = {};
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      });
    },
  }
}
</script>

<style>
div{
  text-align: left;
}
</style>